package com.jwpdk.common;

import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import com.jwpdk.util.StringUtils;
/**
 * 
 * @author 王志伟
 *
 */
public final class Config {

	private final Logger log = Logger.getLogger(this.getClass());

	public static final String SYSTEM_MODE_DEBUG = "debug";

	public static final String SYSTEM_MODE_PROD = "prod";

	public static final int DB_TYPE_ORACLE = -1008861826;

	public static final int DB_TYPE_MYSQL = 104382626;

	public static final int DB_TYPE_SQLSERVER = -1874470255;

	public static final String DB_CON_JNDI = "jndi";

	public static final String DB_CON_JDBC = "jdbc";

	public static final String SQLMAP_NON = "non";

	public static final String ACTIONIMPTYPE_FILE = "file";

	public static final String ACTIONIMPTYPE_DB = "db";

	private static Config config;

	/**
	 * 获取ACTION的后缀名
	 * 
	 * @return
	 */
	public String getActionExtension() {
		return config.getkey("action.extension");
	}

	/**
	 * 获取action存储映射
	 * 
	 * @return
	 */
	public String getDispatcher() {
		return config.getkey("action.dispatcher");
	}

	/**
	 * 获取action实现方式
	 * 
	 * @return
	 */
	public String getActionImpType() {
		return config.getkey("action.actionimptype");
	}

	/**
	 * 是否不启用sqlmap配置
	 * 
	 * @return
	 */
	public boolean isNonSqlMap() {
		return "".equals(getSqlMapTable())
				|| SQLMAP_NON.equalsIgnoreCase(getSqlMapTable()) ? true : false;
	}

	/**
	 * 获取SQL_map映射表。
	 * 
	 * @return
	 */
	public String getSqlMapTable() {
		return StringUtils.trim(config.getkey("sqlmap"));
	}

	/**
	 * 获取SQL_map映射域名。
	 * 
	 * @return
	 */
	public String[] getSqlMapDomain() {
		if (config.getkey("domainname") == null)
			return null;
		String domain = config.getkey("domainname");
		return StringUtils.getArrayFromString(domain, ",");
	}

	/**
	 * 获取用户加载Cache信息
	 * 
	 * @return
	 */
	public String getCachePath() {
		String path = config.getkey("cachefile");
		return !StringUtils.IsEmptyOrNull(path) && !path.startsWith("/") ? "/" + path : path;
	}

	/**
	 * 获取JNDI串
	 * 
	 * @return
	 */
	public String getJNDIStr() {
		return "java:comp/env/jdbc/" + config.getkey("dbjndi");
	}

	/**
	 * 获取数据库参数信息
	 * 
	 * @return
	 */
	public Properties getDBParameters() {
		Properties prop = new Properties();
		prop.put("user", config.getkey("user"));
		prop.put("password", config.getkey("password"));
		return prop;

	}

	/**
	 * 获取DAO实现类
	 * 
	 * @return
	 */
	public String getDAOImpl() {
		return StringUtils.trim(config.getkey("daoimpl"));

	}

	/**
	 * 获取数据库URI
	 * 
	 * @return
	 */
	public String getDBURL() {
		return config.getkey("dburl");
	}

	/**
	 * 获取数据库连接方式
	 * 
	 * @return
	 */
	public String getDBMode() {
		return config.getkey("dbconmode");
	}

	/**
	 * 获取数据库种类
	 * 
	 * @return
	 */
	public int getDBType() {
		return config.getkey("dbtype").toLowerCase().hashCode();
	}

	/**
	 * 所有配置
	 */
	private Hashtable<String, String> hsCofing;

	/**
	 * 默认配置
	 */
	private ResourceBundle dfConfig;

	/**
	 * 用户配置
	 */
	private ResourceBundle jwpdkConfig;

	public static Config getInstance() {
		if (config == null
				|| config.getSysMode().equalsIgnoreCase(SYSTEM_MODE_DEBUG)) {
			config = new Config();
		}
		return config;
	}

	/**
	 * 根据key获取值
	 * 
	 * @param key
	 * @return
	 */
	public String getkey(String key) {

		return hsCofing.get(key).toString();
	}

	/**
	 * 是否存在KEY
	 * 
	 * @param key
	 * @return
	 */
	public boolean contains(String key) {
		return hsCofing.containsKey(key);
	}

	/**
	 * 加载config.properties
	 */
	private Config() {
		// 加载默认配置
		hsCofing = new Hashtable<String, String>();
		dfConfig = ResourceBundle.getBundle("default");
		Enumeration enu = dfConfig.getKeys();
		String key_name;
		if (enu != null) {
			while (enu.hasMoreElements()) {
				key_name = String.valueOf(enu.nextElement());
				hsCofing.put(key_name, dfConfig.getString(key_name));
			}
		}
		log.debug("dfConfig:" + hsCofing);
		// 加载用户配置
		URL url = Thread.currentThread().getContextClassLoader().getResource("jwpdk.properties");
		// String path =
		// Config.class.getClassLoader().getResource("jwpdk.properties").getPath();
		// File file = new File(path + "jwpdk.properties");
		if (url != null) {
			jwpdkConfig = ResourceBundle.getBundle("jwpdk");
			enu = jwpdkConfig.getKeys();
			if (enu != null) {
				while (enu.hasMoreElements()) {
					key_name = String.valueOf(enu.nextElement());
					hsCofing.put(key_name, jwpdkConfig.getString(key_name));
				}
			}
		}
		log.debug("newConfig:" + hsCofing);
	}

	/**
	 * 获取系统模式 系统模式 debug|prod debug:相关的配置文件不加载缓存 prod:加载进缓存
	 * 
	 * @return
	 */
	public String getSysMode() {
		return hsCofing.get("systemmode");
	}
}
